<!--
 * @Description: 食谱页面
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-04-09 17:42:09
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-04-26 16:42:48
 -->
<template>
  <div class="app-container">
    <eHeader :query="query"/>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" border style="width: 100%;">
      <el-table-column prop="id" label="ID" width="85" />
      <el-table-column prop="name" label="食谱名称" />
      <el-table-column prop="service_day" label="食谱天数" />
      <el-table-column prop="energy" label="食谱热量" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '启用' : '禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" />
      <el-table-column prop="updated_at" label="修改时间" />
      <el-table-column label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <edit v-if="checkPermission(['ADMIN'])" :data="scope.row" :sup_this="sup_this"/>
          <el-popconfirm v-if="+scope.row.status === 1" title="确定禁用当前食谱吗？" @onConfirm="changeStatus(scope.row.id, 0)">
            <el-button slot="reference" type="danger" size="mini">禁用</el-button>
          </el-popconfirm>
          <el-popconfirm v-if="+scope.row.status === 0" title="确定启用当前食谱吗？" @onConfirm="changeStatus(scope.row.id, 1)">
            <el-button slot="reference" type="primary" size="mini">启用</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'

export default {
  components: { eHeader, edit },
  mixins: [initData],
  data() {
    return {
      delLoading: false, sup_this: this
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = '/admin/v1/recipe'
      const sort = 'id,desc'
      this.params = { page: this.page, limit: this.limit, sort: sort }
      const query = this.query
      const type = query.type
      const value = query.value
      if (type && value) { this.params[type] = value }
      return true
    },
    changeStatus(id, status) {
      this.$api.resource.recipe
        .changeStatus({
          data: { id: id, status: status }
        })
        .then(res => {
          this.delLoading = false
          this.init()
          this.$notify({ title: '食谱操作成功', type: 'success', duration: 2500 })
        }).catch(err => {
          this.delLoading = false
          console.log(err.response.data.message)
        })
    }
  }
}
</script>

<style scoped>

</style>
